﻿using System;
using Couchbase;
using Enyim.Caching.Memcached;
using System.Runtime.Caching;

namespace Century.EnterpriseLibrary.Caching
{
    public class MemoryCacheManager : ICacheManager
    {
        public static readonly MemoryCacheManager Instance = new MemoryCacheManager();

        private MemoryCacheManager()
        {

        }

        protected ObjectCache Cache
        {
            get
            {
                return MemoryCache.Default;
            }
        }

        public object Get<T>(string key)
        {
            return Cache[key.Replace(" ", "").ToLower()];
        }

        public object Get<T>(string key, string region)
        {
            return Cache[(key + region).Replace(" ", "").ToLower()];
        }

        public void Remove(string key)
        {
            Cache.Remove(key.Replace(" ", "").ToLower());
        }

        public void Set(string key, object value)
        {
            if (value == null)
            {
                return;
            }
            Cache.Set(key.Replace(" ", "").ToLower(), value, new DateTimeOffset(DateTime.Now + TimeSpan.FromMinutes(15)));
        }

        public void Set(string key, object value, string region)
        {
            if (value == null)
            {
                return;
            }
            Cache.Set(key.Replace(" ", "").ToLower(), value, new DateTimeOffset(DateTime.Now + TimeSpan.FromMinutes(15)), region);
        }

        public void Set(string key, object value, TimeSpan expiration)
        {
            if (value == null)
            {
                return;
            }
            Cache.Set(key.Replace(" ", "").ToLower(), value, new DateTimeOffset(DateTime.Now.Add(expiration)));

        }

        public void Set(string key, object value, string region, TimeSpan expiration)
        {
            if (value == null)
            {
                return;
            }
            Cache.Set(key.Replace(" ", "").ToLower(), value, new DateTimeOffset(DateTime.Now.Add(expiration)), region);
        }

    }
}
